package com.cro.prison.controller;


import com.alibaba.fastjson.JSON;
import com.cro.prison.entity.constant.DISCIPLINE_PRISONER;
import com.cro.prison.entity.pojo.Discipline;
import com.cro.prison.entity.vo.BehaviorChooseVo;
import com.cro.prison.entity.vo.DailyBehaviorVo;
import com.cro.prison.entity.vo.DisciplineVo;
import com.cro.prison.service.DisciplineService;
import com.cro.prison.utils.Code;
import com.cro.prison.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/discipline")
public class DisciplineController implements Serializable {


    @Autowired
    DisciplineService disciplineService;


    //对应日常行为表，返回当前时间段对应的异常行为记录
    //参数，时间段，personId
    @GetMapping("/getDiscipline")
    public Result getDiscipline(Long personId, String beginDate,String endDate){
        Date begin = Date.valueOf(beginDate);
        Date end = Date.valueOf(endDate);
        if(personId == -1){
            List<DisciplineVo> voList = disciplineService.getAllDiscipline(begin,end);
            Object json = JSON.toJSON(voList);
            return new Result(Code.GET_OK,json);
        }
//        Long id = Long.parseLong(personId);
        List<DisciplineVo> voList = disciplineService.getDiscipline(personId,begin,end);
        Object json = JSON.toJSON(voList);
        return new Result(Code.GET_OK,json);
    }

    @GetMapping("/getDisciplineByPersonIdAndDate")
    public Result getDisciplineByPersonIdAndDate(Long personId,Date date,String abnormalBehaviorType){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        Integer type = DISCIPLINE_PRISONER.getCode(abnormalBehaviorType);
        List<Discipline> list = disciplineService.getDisciplineByPersonIdAndDate(personId,date,type);
        List<BehaviorChooseVo> resultList = list.stream().map(item -> {
            BehaviorChooseVo behaviorChooseVo = new BehaviorChooseVo();
            behaviorChooseVo.setDate(simpleDateFormat.format(item.getCreateTime()));
            behaviorChooseVo.setType(DISCIPLINE_PRISONER.getValue(item.getAbnormalBehaviorType()));
            behaviorChooseVo.setRecordUrl(item.getRecordUrl());
            return behaviorChooseVo;
        }).collect(Collectors.toList());
        return new Result(Code.GET_OK,resultList);
    }

}
